<template>
  <div class="search-suggestion">
    <van-cell icon="search"
              :key="index"
              v-for="(item,index) in suggestions"
             @click="$emit('search',item)">
    <div slot="title" v-html="highlight(item)"></div>
    </van-cell>
  </div>
</template>

<script>
import {getSearchSuggestions} from "@/api/search";
import {debounce} from 'lodash'


export default {
  name: "SearchSuggestion",
  props:{
    searchText:{
      type:String,
      required:true
    }
  },
  data(){
    return{
      suggestions:[]
    }
  },
  watch:{
    searchText:{
       handler:debounce(async function (){
      const {data}=await getSearchSuggestions(this.searchText)
      this.suggestions=data.data.options
    },200),
      immediate:true
    }
  },
  methods:{
    highlight(str){
      return str.replace(new RegExp(this.searchText,'gi'),
          `<span style="color:red">${this.searchText}</span>>`)
    }
  }
}
</script>

<style scoped>

</style>